package com.wispx.gateway.core.filter.gray;

import com.wispx.gateway.core.filter.gray.strategy.GrayStrategy;
import com.wispx.gateway.core.filter.gray.strategy.ThresholdGrayStrategy;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

/**
 * 灰度策略管理
 */
@Slf4j
public class GrayStrategyManager {

    private static final Map<String, GrayStrategy> grayStrategyMap = new HashMap<>();

    static {
        ServiceLoader<GrayStrategy> grayStrategies = ServiceLoader.load(GrayStrategy.class);
        for (GrayStrategy grayStrategy : grayStrategies) {
            if (grayStrategy == null) continue;
            grayStrategyMap.put(grayStrategy.mark(), grayStrategy);
            log.info("success load gray strategy:{}", grayStrategy.mark());
        }
    }

    private GrayStrategyManager() {

    }

    public static GrayStrategy getGrayStrategy(String strategyName) {
        return grayStrategyMap.getOrDefault(strategyName, new ThresholdGrayStrategy());
    }

}
